import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:osg/controllers/http.dart';
import 'package:osg/controllers/my_method.dart';

class Assets extends StatefulWidget {
  const Assets({super.key});
  @override
  State<Assets> createState() => _HomePageState();
}

RxList listDate = [].obs;
RxList skuList = [].obs;
RxMap userInfo = {}.obs;
RxBool piLiangFlag = false.obs;

RxString istype = ''.obs;

class _HomePageState extends State<Assets> with SingleTickerProviderStateMixin {
  late TabController _tabController;

  RxMap infoDate = {}.obs;

  getList(type) {
    Get.find<HTTPS>().getData('/packages/list', {'type': type}).then((res) {
      listDate.value = res['list'];
      infoDate.value = res;
    });
  }

  getUserinfo() {
    Get.find<HTTPS>().getData('/userInfo', {}).then((res) {
      userInfo.value = res;
      Get.find<MyMethod>().myset('user', res);
    });
  }

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 3, vsync: this);

    WidgetsBinding.instance.addPostFrameCallback((_) {
      getList('product');
      getUserinfo();
    });
  }

  @override
  Widget build(BuildContext context) {
    final totalHeight = MediaQuery.of(context).padding.top + 15;

    final screenWidth = MediaQuery.of(context).size.width;
    final spacing = 12.0; // 间距
    final itemWidth = (screenWidth - spacing) / 2;
    const itemHeight = 180.0; // 固定高度

    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.dark,
      child: Scaffold(
        body: ListView(
          padding: EdgeInsets.only(top: totalHeight, right: 15, left: 15),
          children: [
            Stack(
              children: [
                Column(
                  children: [
                    Container(
                      padding: EdgeInsets.only(
                          bottom: 35, right: 15, left: 15, top: 15),
                      decoration: BoxDecoration(
                          color: Color(0XFF0F172B),
                          borderRadius: BorderRadius.circular(15)),
                      child: Row(
                        children: [
                          Image.asset(
                            'static/web/shuzi.png',
                            width: 33,
                            fit: BoxFit.fitWidth,
                          ),
                          SizedBox(
                            width: 10,
                          ),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text('SP',
                                  style: TextStyle(
                                      fontSize: 18, color: Color(0XFFFFFFFF))),
                              Text('数字积分',
                                  style: TextStyle(
                                      fontSize: 11, color: Color(0XFFFFFFFF))),
                            ],
                          ),
                          Expanded(child: SizedBox()),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.end,
                            children: [
                              Text('0',
                                  style: TextStyle(
                                      fontSize: 18, color: Color(0XFFFFFFFF))),
                              Text('可用',
                                  style: TextStyle(
                                      fontSize: 11, color: Color(0XFFFFFFFF))),
                            ],
                          ),
                        ],
                      ),
                    ),
                    Transform.translate(
                      offset: Offset(0, -20),
                      child: Container(
                          height: 130,
                          padding: EdgeInsets.symmetric(
                              horizontal: 15, vertical: 10),
                          decoration: BoxDecoration(
                              color: Color(0XFF542AFF),
                              borderRadius: BorderRadius.circular(15)),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Row(
                                children: [
                                  Image.asset(
                                    'static/web/st.png',
                                    width: 33,
                                    fit: BoxFit.fitWidth,
                                  ),
                                  SizedBox(
                                    width: 10,
                                  ),
                                  Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Text('ST',
                                          style: TextStyle(
                                              fontSize: 18,
                                              color: Color(0XFFFFFFFF))),
                                      Text('平台积分',
                                          style: TextStyle(
                                              fontSize: 11,
                                              color: Color(0XFFFFFFFF))),
                                    ],
                                  ),
                                  Expanded(child: SizedBox()),
                                  Column(
                                    crossAxisAlignment: CrossAxisAlignment.end,
                                    children: [
                                      Text('0',
                                          style: TextStyle(
                                              fontSize: 18,
                                              color: Color(0XFFFFFFFF))),
                                      Text('总计',
                                          style: TextStyle(
                                              fontSize: 11,
                                              color: Color(0XFFFFFFFF))),
                                    ],
                                  ),
                                ],
                              ),
                              Row(
                                children: [
                                  Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Text('可用余额',
                                          style: TextStyle(
                                              fontSize: 11,
                                              color: Color(0XFFFFFFFF))),
                                      Text('0',
                                          style: TextStyle(
                                              fontSize: 14,
                                              color: Color(0XFFFFFFFF))),
                                    ],
                                  ),
                                  Expanded(child: SizedBox()),
                                  Row(
                                    children: [
                                      GestureDetector(
                                        onTap: () => {
                                          Get.toNamed(
                                              '/web_tabs/shopping/mall_amount?type=1')
                                        },
                                        child: Text('提现',
                                            style: TextStyle(
                                                fontSize: 14,
                                                color: Color(0XFFFFFFFF))),
                                      ),
                                      SizedBox(
                                        width: 20,
                                      ),
                                      Container(
                                        height: 13,
                                        width: 1,
                                        color: Color(0XFF9A75FF),
                                      ),
                                      SizedBox(
                                        width: 20,
                                      ),
                                      GestureDetector(
                                        onTap: () => {
                                          Get.toNamed(
                                              '/web_tabs/shopping/mall_amount?type=2')
                                        },
                                        child: Text('充值',
                                            style: TextStyle(
                                                fontSize: 14,
                                                color: Color(0XFFFFFFFF))),
                                      ),
                                    ],
                                  ),
                                ],
                              ),
                            ],
                          )),
                    ),
                  ],
                ),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                GestureDetector(
                  onTap: () =>
                      {Get.toNamed('/web_tabs/shopping/mall_integral')},
                  child: Column(children: [
                    Image.asset(
                      'static/web/jifen.png',
                      width: 44,
                      fit: BoxFit.fitWidth,
                    ),
                    Text('账户明细',
                        style:
                            TextStyle(fontSize: 12, color: Color(0XFF0F172B))),
                  ]),
                ),
                GestureDetector(
                  onTap: () {Get.toNamed('/web_tabs/order/order_list');},
                  child: Column(children: [
                    Image.asset(
                      'static/web/cangping.png',
                      width: 44,
                      fit: BoxFit.fitWidth,
                    ),
                    Text('藏品订单',
                        style:
                            TextStyle(fontSize: 12, color: Color(0XFF0F172B))),
                  ]),
                ),
                GestureDetector(
                  onTap: () => {Get.toNamed('/web_tabs/shopping/mall_wallet')},
                  child: Column(children: [
                    Image.asset(
                      'static/web/qianbao.png',
                      width: 44,
                      fit: BoxFit.fitWidth,
                    ),
                    Text('钱包',
                        style:
                            TextStyle(fontSize: 12, color: Color(0XFF0F172B))),
                  ]),
                ),
                GestureDetector(
                  onTap: () => {Get.toNamed('/web_tabs/shopping/mall')},
                  child: Column(children: [
                    Image.asset(
                      'static/web/spshop.png',
                      width: 44,
                      fit: BoxFit.fitWidth,
                    ),
                    Text('SP商城',
                        style:
                            TextStyle(fontSize: 12, color: Color(0XFF0F172B))),
                  ]),
                ),
              ],
            ),
            SizedBox(
              height: 15,
            ),
            TabBar(
                onTap: (vlaue) => {
                      listDate.value = [],
                      if (vlaue == 0)
                        {getList('product')}
                      else if (vlaue == 1)
                        {getList('box')}
                      else
                        {getList('spe')}
                    },
                indicatorColor: Color(0XFF0F172B),
                indicatorWeight: 3.0,
                labelPadding: EdgeInsets.symmetric(vertical: 10),
                indicatorSize: TabBarIndicatorSize.tab,
                labelColor: Color(0XFF0F172B),
                unselectedLabelColor: Color(0xff999999),
                dividerColor: Color(0XFFF8F9FD),
                dividerHeight: 3,
                controller: _tabController,
                overlayColor: WidgetStateProperty.all(Colors.transparent),
                tabs: [
                  Obx(
                    () => Text(
                      '藏品(${infoDate['count_product']})',
                      style: TextStyle(fontSize: 14),
                    ),
                  ),
                  Obx(
                    () => Text(
                      '盲盒(${infoDate['count_box']})',
                      style: TextStyle(fontSize: 14),
                    ),
                  ),
                  Obx(
                    () => Text(
                      '道具(${infoDate['count_spc']})',
                      style: TextStyle(fontSize: 14),
                    ),
                  ),
                ]),
            Lise(spacing, itemWidth, itemHeight)
          ],
        ),
      ),
    );
  }
}

Widget Lises(spacing, itemWidth, itemHeight) {
  return Obx(() => Container(
        color: Color(0XFFFFFFFF),
        child: GridView.count(
          crossAxisCount: 2,
          crossAxisSpacing: spacing,
          mainAxisSpacing: spacing,
          childAspectRatio: itemWidth / itemHeight,
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
          children: List.generate(skuList.length, (index) {
            int type;
            return GestureDetector(
              onTap: () => {
                if (piLiangFlag.value)
                  {
                    skuList[index]['falg'] = !skuList[index]['falg'],
                    skuList.refresh()
                  }
                else
                  {
                    // ?is_box=0&uuid=6824afb118872
                    type = istype.value == 'box' ? 1 : 0,
                Get.back(),
                    Get.toNamed(
                        '/web_tabs/resale/detail?is_box=$type&uuid=${skuList[index]['uuid']}')

                  }
              },
              child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 12, vertical: 10),
                  decoration: BoxDecoration(
                    border: Border.all(
                        color: skuList[index]['falg']
                            ? Color(0XFF542AFF)
                            : Color(0XFFffffff),
                        width: 2),
                    borderRadius: BorderRadius.circular(10),
                    color: Color(0XFFF8F9FD),
                  ),
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            Text(
                              '#${Get.find<MyMethod>().hao('${skuList[index]['nftId']}')}',
                              style: TextStyle(
                                  fontSize: 16,
                                  color: Color(0XFF0F172B),
                                  fontWeight: FontWeight.bold),
                            ),
                            if (skuList[index]['status'] == 5)
                              Container(
                                margin: EdgeInsets.only(left: 7),
                                padding: EdgeInsets.symmetric(horizontal: 3),
                                decoration: BoxDecoration(
                                    border:
                                        Border.all(color: Color(0XFF542AFF)),
                                    borderRadius: BorderRadius.circular(5)),
                                child: Text('寄售中',
                                    style: TextStyle(
                                        fontSize: 12,
                                        color: Color(0XFF542AFF))),
                              ),
                            Expanded(child: SizedBox()),
                            if (piLiangFlag.value)
                              Image.asset(
                                skuList[index]['falg']
                                    ? 'static/web/shous.png'
                                    : 'static/web/shou.png',
                                width: 17,
                                fit: BoxFit.fitWidth,
                              )
                          ],
                        ),
                        SizedBox(
                          height: 5,
                        ),

                        Text(
                          '寄售价：￥${skuList[index]['status'] == 5 ? skuList[index]['sell_price'] : '-'}',
                          style:
                              TextStyle(fontSize: 14, color: Color(0XFF999999)),
                        ),
                        // Text(
                        //   '寄售价：￥3235.33 ',
                        //   style: TextStyle(fontSize: 14, color: Color(0XFF999999)),
                        // ),
                      ])),
            );
          }),
        ),
      ));
}

Widget Lise(spacing, itemWidth, itemHeight) {
  return Obx(() => GridView.count(
        crossAxisCount: 2,
        crossAxisSpacing: spacing,
        mainAxisSpacing: spacing,
        childAspectRatio: itemWidth / itemHeight,
        shrinkWrap: true,
        physics: NeverScrollableScrollPhysics(),
        children: List.generate(listDate.length, (index) {
          return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                GestureDetector(
                  onTap: () {
                    istype.value = listDate[index]['type'];
                    Get.find<HTTPS>().getData('/packages/sku_list', {
                      'product_id': '${listDate[index]['product_id']}',
                      'per_page': '999',
                      'page': '1'
                    }).then((res) {
                      for (var i = 0; i < res['list'].length; i++) {
                        res['list'][i]['falg'] = false;
                      }

                      skuList.value = res['list'];

                      if (res != null) {
                        Get.bottomSheet(
                            isScrollControlled: true,
                            Container(
                              height: 500,
                              padding: EdgeInsets.symmetric(horizontal: 15),
                              child: Column(children: [
                                SizedBox(
                                  height: 20,
                                ),
                                Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        Row(
                                          children: [
                                            Text('${listDate[index]['title']}',
                                                style: TextStyle(
                                                    fontSize: 16,
                                                    color: Color(0XFF0F172B))),
                                            Text(
                                                '(共${listDate[index]['count']}份)',
                                                style: TextStyle(
                                                    fontSize: 14,
                                                    color: Color(0XFF542AFF))),
                                          ],
                                        ),
                                      ],
                                    ),
                                    GestureDetector(
                                      onTap: () {
                                        Get.back();
                                      },
                                      child: Image.asset(
                                        'static/close.png',
                                        width: 18,
                                        fit: BoxFit.fitWidth,
                                      ),
                                    )
                                  ],
                                ),
                                SizedBox(
                                  height: 15,
                                ),
                                Obx(() => Row(
                                      children: [
                                        userInfo['multiMany'] == 1 &&
                                                !piLiangFlag.value
                                            ? GestureDetector(
                                                onTap: () {
                                                  print('object');
                                                  piLiangFlag.value = true;
                                                },
                                                child: Container(
                                                  width: 80,
                                                  height: 30,
                                                  alignment: Alignment.center,
                                                  decoration: BoxDecoration(
                                                      border: Border.all(
                                                          color: Color(
                                                              0XFF0F172B)),
                                                      color: Color(0XFF0F172B),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              20)),
                                                  child: Text('批量寄售',
                                                      style: TextStyle(
                                                          fontSize: 12,
                                                          color: Color(
                                                              0XFFFFFFFF))),
                                                ),
                                              )
                                            : SizedBox(),
                                        userInfo['multiMany'] == 1 &&
                                                piLiangFlag.value
                                            ? GestureDetector(
                                                onTap: () {
                                                  piLiangFlag.value = false;

                                                  for (var i = 0;
                                                      i < skuList.length;
                                                      i++) {
                                                    skuList[i]['falg'] = false;
                                                  }
                                                },
                                                child: Container(
                                                  width: 80,
                                                  height: 30,
                                                  alignment: Alignment.center,
                                                  decoration: BoxDecoration(
                                                      border: Border.all(
                                                          color: Color(
                                                              0XFF999999)),
                                                      color: Color(0XFF999999),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              20)),
                                                  child: Text('取消批量',
                                                      style: TextStyle(
                                                          fontSize: 12,
                                                          color: Color(
                                                              0XFFFFFFFF))),
                                                ),
                                              )
                                            : SizedBox(),
                                        SizedBox(
                                          width: 15,
                                        ),
                                        GestureDetector(
                                          onTap: () {
                                            Get.dialog(
                                              AlertDialog(
                                                backgroundColor:
                                                    Colors.transparent,
                                                content: Stack(
                                                  children: [
                                                    Image.asset(
                                                      'static/tan.png',
                                                      width: 250,
                                                      fit: BoxFit.fitWidth,
                                                    ),
                                                    Positioned(
                                                        top: 80,
                                                        left: 0,
                                                        right: 0,
                                                        child: Align(
                                                            child: Column(
                                                          children: [
                                                            Text(
                                                              '提示',
                                                              style: TextStyle(
                                                                  fontSize: 24,
                                                                  fontWeight:
                                                                      FontWeight
                                                                          .bold),
                                                            ),
                                                            SizedBox(
                                                              height: 15,
                                                            ),
                                                            Text(
                                                              '确定批量取消寄售吗',
                                                              style: TextStyle(
                                                                  fontSize: 12,
                                                                  fontWeight:
                                                                      FontWeight
                                                                          .bold),
                                                            ),
                                                            GestureDetector(
                                                              onTap: () {
                                                                Get.find<
                                                                        HTTPS>()
                                                                    .postData(
                                                                        '/resale/closeAll',
                                                                        {
                                                                      "product_id":
                                                                          listDate[index]
                                                                              [
                                                                              'product_id']
                                                                    }).then(
                                                                        (res) {
                                                                  if (res !=
                                                                      null) {
                                                                    Get.back();
                                                                    Get.back();
                                                                    Get.find<
                                                                            HTTPS>()
                                                                        .showTost(
                                                                            '取消成功');
                                                                  }
                                                                });
                                                              },
                                                              child: Container(
                                                                height: 50,
                                                                margin: EdgeInsets
                                                                    .only(
                                                                        top: 50,
                                                                        bottom:
                                                                            30),
                                                                alignment:
                                                                    Alignment
                                                                        .center,
                                                                width: 210,
                                                                decoration: BoxDecoration(
                                                                    color: Color(
                                                                        0XFF0F172B),
                                                                    borderRadius:
                                                                        BorderRadius.circular(
                                                                            50)),
                                                                child: Text(
                                                                    '确认',
                                                                    style: TextStyle(
                                                                        fontSize:
                                                                            16,
                                                                        color: Color(
                                                                            0XFFFFFFFF))),
                                                              ),
                                                            ),
                                                            GestureDetector(
                                                              onTap: () =>
                                                                  {Get.back()},
                                                              child: Text(
                                                                '取消',
                                                                style: TextStyle(
                                                                    fontSize:
                                                                        18,
                                                                    fontWeight:
                                                                        FontWeight
                                                                            .bold),
                                                              ),
                                                            )
                                                          ],
                                                        )))
                                                  ],
                                                ),
                                              ),
                                            );
                                          },
                                          child: Container(
                                            width: 80,
                                            height: 30,
                                            alignment: Alignment.center,
                                            decoration: BoxDecoration(
                                                border: Border.all(
                                                    color: Color(0XFF0F172B)),
                                                borderRadius:
                                                    BorderRadius.circular(20)),
                                            child: Text('批量下架',
                                                style: TextStyle(
                                                    fontSize: 12,
                                                    color: Color(0XFF0F172B))),
                                          ),
                                        ),
                                      ],
                                    )),
                                Expanded(
                                    child: ListView(
                                  padding: EdgeInsets.symmetric(vertical: 15),
                                  children: [Lises(spacing, itemWidth, 85)],
                                )),
                                Obx(() => Container(
                                      child: userInfo['multiMany'] == 1 &&
                                              piLiangFlag.value
                                          ? GestureDetector(
                                              onTap: () {
                                                var arr = [];
                                                for (var i = 0;
                                                    i < skuList.length;
                                                    i++) {
                                                  if (skuList[i]['falg']) {
                                                    arr.add(skuList[i]['id']);
                                                  }
                                                }
                                                if (arr.isNotEmpty) {
                                                  Get.toNamed(
                                                      '/web_tabs/resale/resale?uuid=${skuList[0]['uuid']}&piliang=1&arr=$arr');
                                                  print(arr);
                                                } else {
                                                  Get.find<HTTPS>()
                                                      .showTost('请选择需要寄售的物品');
                                                }
                                              },
                                              child: Container(
                                                margin: EdgeInsets.symmetric(
                                                    vertical: 10),
                                                decoration: BoxDecoration(
                                                    color: Color(0XFF0F172B),
                                                    borderRadius:
                                                        BorderRadius.circular(
                                                            50)),
                                                height: 50,
                                                alignment: Alignment.center,
                                                child: Text(
                                                  '一键寄售',
                                                  style: TextStyle(
                                                      fontSize: 16,
                                                      color: Color(0XFFFFFFFF)),
                                                ),
                                              ),
                                            )
                                          : userInfo['multiMany'] == 1 &&
                                                  !piLiangFlag.value
                                              ? Container(
                                                  margin: EdgeInsets.symmetric(
                                                      vertical: 10),
                                                  decoration: BoxDecoration(
                                                      color: Color(0XFFd8d8d8),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              50)),
                                                  height: 50,
                                                  alignment: Alignment.center,
                                                  child: Text(
                                                    '一键寄售',
                                                    style: TextStyle(
                                                        fontSize: 16,
                                                        color:
                                                            Color(0XFF999999)),
                                                  ),
                                                )
                                              : SizedBox(),
                                    ))
                              ]),
                            ),
                            shape: RoundedRectangleBorder(
                              // 圆角设置:ml-citation{ref="7" data="citationList"}
                              borderRadius: BorderRadius.vertical(
                                  top: Radius.circular(15)),
                            ),
                            backgroundColor: Colors.white);
                      }
                    });
                  },
                  child: Stack(
                    children: [
                      ClipRRect(
                        borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(15),
                          topRight: Radius.circular(15),
                        ),
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(15.0), // 设置圆角大小
                          child: Image.network('${listDate[index]['thumb']}',
                              width: 162, height: 162, fit: BoxFit.fill,
                              errorBuilder: (context, error, stackTrace) {
                            return SizedBox(
                              width: 162,
                            );
                          }),
                        ),
                      ),
                      Positioned(
                        bottom: 10,
                        right: 10,
                        child: Container(
                          padding:
                              EdgeInsets.symmetric(horizontal: 10, vertical: 2),
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(20),
                              color: Color.fromARGB(128, 0, 0, 0)),
                          child: Row(
                            children: [
                              Image.asset(
                                'static/web/shulaing.png',
                                width: 14,
                                fit: BoxFit.fitWidth,
                              ),
                              SizedBox(
                                width: 5,
                              ),
                              Text('${listDate[index]['count']}',
                                  style: TextStyle(
                                      fontSize: 14, color: Color(0XFFFFFFFF))),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ]);
        }),
      ));
}
